<template>
  <div class="app-container">
    <!-- 执行器管理 -->
    <el-row :gutter="10" class="mb8">
      <add-button @click="handleAdd" :perm="['job:executor:add']" />
      <right-toolbar v-model:showSearch="showSearch" @query-table="getList" />
    </el-row>
    <el-table v-loading="loading" :data="executorList">
      <el-table-column prop="id" label="ID" align="center" width="120" />
      <el-table-column prop="appName" label="AppName" align="center" min-width="160" />
      <el-table-column prop="title" label="名称" align="center" min-width="160" />
      <el-table-column prop="addressType" label="注册方式" align="center" width="80">
        <template #default="scope">
          <span v-if="scope.row.addressType==='0'">
            <el-tag type="info">自动注册</el-tag>
          </span>
          <span v-if="scope.row.addressType==='1'">
            <el-tag>手动录入</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="在线机器地址" align="center" min-width="240">
        <template #default="scope">
          <span v-if="scope.row.registryList">
            <el-tag effect="plain" type="success" v-for="(add,index) in scope.row.registryList" :key="index" class="ml10"><strong>{{ add }}</strong></el-tag>
          </span>
          <span class="text-danger" v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template #default="scope">
          <text-edit-button @click="handleUpdate(scope.row)" :perm="['job:executor:edit']" />
          <text-delete-button @click="handleDelete(scope.row)" :perm="['job:executor:remove']" :title="'是否确认删除'+scope.row.title+'?'" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    <!-- 新增/修改执行器 -->
    <drag-dialog v-el-drag-dialog :title="title" :close-on-click-modal="false" v-model="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="AppName" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入执行器的AppName" />
        </el-form-item>
        <el-form-item label="名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入执行器名称" />
        </el-form-item>
        <el-form-item label="注册方式">
          <el-radio-group v-model="form.addressType">
            <el-radio v-model="form.addressType" label="0">自动注册</el-radio>
            <el-radio v-model="form.addressType" label="1">手动录入</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="机器地址" prop="addressList">
          <el-input
            type="textarea" :autosize="{minRows: 2,maxRows: 4}" :disabled="form.addressType==='0'" placeholder="请输入执行器地址列表，多地址逗号分隔"
            v-model="form.addressList" />
        </el-form-item>
      </el-form>
      <template #footer>
        <dialog-footer @ok="submitForm" @cancel="cancel" />
      </template>
    </drag-dialog>
  </div>
</template>

<script>
import { listExecutor, getExecutor, addExecutor, updateExecutor, delExecutor } from "@job/api/job/group";
import { debounceWrapper } from "@/utils/zmids";

export default {
  name: "JobGroup",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 表格树数据
      executorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        appName: [
          { required: true, message: "应用不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "名称不能为空", trigger: "blur" },
        ],
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询执行器列表
    getList: debounceWrapper(function () {
      this.loading = true;
      listExecutor(this.queryParams).then(
        response => {
          this.executorList = response.data.list;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    }),
    // 表单重置
    reset() {
      this.form = {
        appName: undefined,
        title: undefined,
        addressType: '0',
        addressList: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.open = true;
      this.title = "新增执行器";
    },
    /** 修改按钮操作 */
    handleUpdate: debounceWrapper(function (row) {
      this.loading = true;
      this.reset();
      const id = row.id
      getExecutor(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改执行器";
      });
    }),
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 提交按钮 */
    submitForm: debounceWrapper(function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.form.id != undefined) {
            updateExecutor(this.form).then(response => {
              this.loading = false;
              if (response.data === 1) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addExecutor(this.form).then(response => {
              this.loading = false;
              if (response.data === 1) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    }),
    /** 删除按钮操作 */
    handleDelete: debounceWrapper(function (row) {
      this.loading = true;
      delExecutor(row.id).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      });
    }),
  }
}
</script>

<style lang="scss" scoped>
</style>